$(function () {
var app = new Vue({
	el: '#app'
	,data:{
		queryIng:false
		,queryPorjectData:{activeFlag:'1',page:1,rows:50}//查询条件
		,queryCaseData:{activeFlag:'1',page:1,rows:50,libraryCode:$("#libraryCode").val(),orderColum:"order_index"}//查询条件
		,caseDatagrid:[]//数据列表数据
		,totalNum:0//分页控件配置
		,moduleData:[]
        ,treeDefaultProps: {
            children: 'children',
            label: 'label'
         }
		,porjectDatagird:[]
		,selectedModuleIndex:-1
		,hoverModuleIndex:-1
		,dialogConfig:{
			formLabelWidth:'120px'
			,projectDialogVisible:false
			,projectAddDialogVisible:false
			,moduleAddDialogVisible:false
			,caseAddDialogVisible:false
			,taskDialogVisible:false
		}
		,libraryName:$("#libraryName").val()
		,addProject:{}
		,addModule:{}
		,addCase:{moduleCode:0,moduleName:'无所属模块'}
		,rules:[]
		,addData:{}
		,isEdit:false
		,operateFormName:''
		,typeData:[]
		,levelData:[]
		,moduleSelectData:[]
		,queryModuleData:{}
		,emptyModule:{id:0,name:'无所属模块'}
		,selectedCase:[]
		,taskDatas:[]
		,selectedTask:''
		,currentCase:null
		,taskGroupDatas:[{id:0,name:'无所属分组'}]
		,selectedTaskGroup:0
	}
	,methods:{
		init:function(){
			this.queryProject();
			this.queryCase();
			this.queryModule();
			this.loadTypeData();
			this.loadLevelData();
			this.queryTask();
		}
		,queryTaskGroup:function(value){
			var vm = this;
			vm.taskGroupDatas=[{id:0,name:'无所属分组'}];
			$.ajax({
				url :staticURL+'/testcase/taskGroup/listAll?taskId='+value,
				dataType : 'json',
				success : function(json) {
					if(json.length){
						vm.taskGroupDatas = vm.taskGroupDatas.concat(json);
					}
				}
			});
		}
		,goBackend:function(){
			window.open(staticURL+'/backend');
		}
		,queryProject:function(){
			var vm = this
			$.ajax({
				url :staticURL+'/testcase/caseProject/listAll.do',
				data : vm.queryPorjectData,
				dataType : 'json',
				success : function(json) {
					app.porjectDatagird = json;
				}
			});
		}
		,queryModule:function(param){
			var vm = this
			var projectId = param;
			if(!param){
				projectId = vm.libraryCode;
			}
			$.ajax({
				url :staticURL+'/testcase/caseModule/listAll.do?activeFlag=1&queryCount=1&projectId='+projectId,
				dataType : 'json',
				success : function(json) {
					if(!param){
						app.moduleData = json;
					}else{
						app.moduleSelectData = json;
					}
				}
			});
		}
		,queryCase:function(){
			var vm = this
			$.ajax({
				url :staticURL+'/testcase/caseLibrary/datagrid.do',
				data : vm.queryCaseData,
				dataType : 'json',
				success : function(json) {
					app.caseDatagrid = json.rows;
					app.totalNum = json.total;
					app.queryIng = false;
					app.currentCase = null;
					window.scrollTo(0,0);  
				}
			});
		}
		,datagridSelect:function(row, event, column){
			window.location.href=staticURL+"/testcase/caseLibrary/goFrontendCaseLibrary"
		}
		,handleSizeChange:function(val){
			app.queryCaseData.rows=val;
			app.queryCase();
		}
		,handleCurrentChange:function(val){
			app.queryCaseData.page=val;
			app.queryCase();
		}
		,handlePrevClick:function(val){
			app.queryCaseData.page=val;
			app.queryCase();
		}
		,handleNextClick:function(val){
			app.queryCaseData.page=val;
			app.queryCase();
		}
		,showProjectMenu:function(){
			app.dialogConfig.projectDialogVisible = true;
		}
		,tableCellStyle:function({row, column, rowIndex, columnIndex}){
			if(columnIndex == 4){
				if(row.level=='P1'){
					return 'color:#F56C6C'
				}
				if(row.level=='P2'){
					return 'color:#409EFF'
				}
				if(row.level=='P3'){
					return 'color:#67C23A'
				}
				if(row.level=='P4'){
					return 'color:#DCDFE6'
				}
				return ''
			}
		}
		,clickModule:function(module,index){
			app.queryModuleData = module;
			if(module){
				app.queryCaseData.moduleCode = module.id;
			}else{
				app.queryCaseData.moduleCode = null;
			}
			app.selectedModuleIndex = index;
			app.queryCase();
			app.hoverModuleIndex = index;
		}
		,projectDatagridSelect:function(row, event, column){
			app.queryCaseData.libraryCode = row.id;
			this.queryCase();
			this.queryModule();
			app.dialogConfig.projectDialogVisible = false;
			app.selectedModuleIndex=-1;
			app.queryCaseData.moduleCode='';
			app.libraryName=row.name;
		}
		,updateProject(){
			app.openCloseDialog('projectAddForm',true);
			app.addProject.name = app.libraryName;
			app.addProject.id = app.libraryCode;
			app.isEdit = true;
			app.addData = app.addProject;
		}
		,deleteProject(){
			app.$confirm('当前正在删除用例库，此操作不可撤销，是否删除？', '删除用例库', {
		          confirmButtonText: '确定',
		          cancelButtonText: '取消',
		          type: 'warning'
	        }).then(function () {
	        	$.ajax({
					url : staticURL+"/testcase/caseProject/deletePt?ids="+app.libraryCode,
					dataType : 'json',
					success : function(r) {
						if (r.success) {
							window.location.href=staticURL+'/frontend'
						} else {
							app.$message({showClose: true,
						          message: r.msg,
						          type: 'error',
						          position: 'bottom-right'});
						}
					}
				});
	        }).catch(function (action) {
	        	  //取消
	        });
		}
		,doAddModule:function(){
			app.openCloseDialog('moduleAddForm',true);
			app.addModule.projectName = app.libraryName;
			app.addModule.projectId = app.libraryCode;
			app.isEdit = false;
			app.addData = app.addModule;
		}
		,doUpdateModule:function(module){
			app.openCloseDialog('moduleAddForm',true);
			app.addModule = JSON.parse(JSON.stringify(module));;
			app.isEdit = true;
			app.addData = app.addModule;
		}
		,doDeleteModule:function(module){
			app.$confirm('当前模块「'+module.name+'」会被删除，用例会被移到「无所属模块」。此操作不可撤销，是否确定？', '删除模块', {
		          confirmButtonText: '确定',
		          cancelButtonText: '取消',
		          type: 'warning'
	        }).then(function () {
	        	app.operateFormName='moduleAddForm';
	        	$.ajax({
					url : staticURL+"/testcase/caseModule/deletePt?ids="+module.id,
					dataType : 'json',
					success : function(r) {
						if (r.success) {
							app.$message({showClose: true,
					          message: r.msg,
					          type: 'success',
					          position: 'bottom-right'});
							app.submitFormSuccess(r);
						} else {
							app.$message({showClose: true,
						          message: r.msg,
						          type: 'error',
						          position: 'bottom-right'});
						}
					}
				});
	        }).catch(function (action) {
	        	  //取消
	        });
		}
		,openCloseDialog:function(formName,openOrNot){
			if(formName=='projectAddForm'){
				app.dialogConfig.projectAddDialogVisible=openOrNot;
			}else if(formName=='moduleAddForm'){
				app.dialogConfig.moduleAddDialogVisible=openOrNot;
			}else if(formName=='caseAddForm'){
				app.dialogConfig.caseAddDialogVisible=openOrNot;
			}else if(formName=='taskForm'){
				app.dialogConfig.taskDialogVisible=openOrNot;
			}
			if(!openOrNot){
				app.resetForm(formName)
			}
			app.operateFormName = formName;
		}
		,resetForm:function(formName) {
			if(app.$refs[formName]){
				app.$refs[formName].resetFields();
			}
	    }
		,submitForm:function(formName){
			app.$refs[formName].validate(function(res){
				if(res){
					//验证通过
					$.ajax({
						url : app.saveUrl,
						data : app.addData,
						dataType : 'json',
						success : function(r) {
							if (r.success) {
								app.$message({showClose: true,
							          message: r.msg,
							          type: 'success',
							          position: 'top'});
								app.openCloseDialog(formName,false);
								app.submitFormSuccess(r);
							} else {
								app.$message({showClose: true,
							          message: r.msg,
							          type: 'error',
							          position: 'top'});
							}
						}
					});
				}
			});
		}
		,submitFormSuccess:function(data){
			if(app.operateFormName=='projectAddForm'){
				app.libraryName = data.obj.name;
				app.queryProject();
			}else if(app.operateFormName=='moduleAddForm'){
				app.queryModule();
			}else if(app.operateFormName=='caseAddForm'){
				app.queryCase();
				app.queryModule();
			}
		}
		,loadTypeData:function(){
			var vm = this
			vm.loadSelectData('A02');
		}
		,loadLevelData:function(){
			var vm = this
			vm.loadSelectData('A03');
		}
		,loadSelectData:function(parentCode){
			var vm = this
			$.ajax({
				url :staticURL+'/basic/baseInfoAction/combox?parentCode='+parentCode,
				dataType : 'json',
				success : function(json) {
					if(parentCode=='A01'){
						vm.libraryData = json;
					}else if(parentCode=='A02'){
						vm.typeData = json;
					}else if(parentCode=='A03'){
						vm.levelData = json;
					}else{
						vm.moduleData = json;
					}
				}
			});
		}
		,datagridDbclick:function(row, event){
			app.openCloseDialog('caseAddForm',true);
			app.addCase = JSON.parse(JSON.stringify(row));
			app.moduleSelectData = JSON.parse(JSON.stringify(app.moduleData));
			app.addData = app.addCase;
			app.isEdit = true;
		}
		,datagridClick:function(row, event, column){
			app.currentCase = row;
		}
		,doAddCase:function(){
			app.openCloseDialog('caseAddForm',true);
			app.isEdit = false;
			app.resetForm('caseAddForm');
			app.addCase = {moduleCode:0,moduleName:'无所属模块'};
			app.addCase.libraryCode = parseInt(app.libraryCode);
			app.addCase.libraryName = app.libraryName;
			if(app.queryModuleData && app.queryModuleData.id){
				app.addCase.moduleCode = app.queryModuleData.id;
				app.addCase.moduleName = app.queryModuleData.name;
			}else{
				app.addCase.moduleCode = app.emptyModule.id;
				app.addCase.moduleName = app.emptyModule.name;
			}
			app.addData = app.addCase;
			app.moduleSelectData = JSON.parse(JSON.stringify(app.moduleData));
			app.addData.currentCaseId = app.currentCaseId;
		}
		,libraryChange:function(data){
			var vm = this
			vm.porjectDatagird.find(function(item){
				if(item.id==data){
					vm.addData.libraryName = item.name
					return;
				}
			})
		    app.addCase.moduleCode = 0
			app.addCase.moduleName = '无所属模块'
			app.queryModule(data);
		}
		,moduleChange:function(data){
			var vm = this;
			vm.moduleSelectData.find(function(item){
				if(item.name==data){
					vm.addData.moduleCode = item.id
					return;
				}
			}) 
		}
		,doDeleteCase:function(){
			app.$confirm('确定删除所选中用例吗？', '删除用例', {
		          confirmButtonText: '确定',
		          cancelButtonText: '取消',
		          type: 'warning'
	        }).then(function () {
	        	var ids = new Array();
				for(var i=0;i<app.selectedCase.length;i++){
					ids.push(app.selectedCase[i].id)
				}
				$.ajax({
					url : staticURL+"/testcase/caseLibrary/deletePt?ids="+ids,
					dataType : 'json',
					success : function(r) {
						if (r.success) {
							app.$message({showClose: true,
						          message: r.msg,
						          type: 'success',
						          position: 'top'});
							app.queryCase();
							app.queryModule();
						} else {
							app.$message({showClose: true,
						          message: r.msg,
						          type: 'error',
						          position: 'top'});
						}
					}
				});
	        }).catch(function (action) {
	        	  //取消
	        });
			
		}
		,handleSelectionChange:function(val){
			app.selectedCase = val;
		}
		,goFrontend:function(){
			window.location.href=staticURL+"/frontend"
		}
		,openTaskDialog:function(){
			app.dialogConfig.taskDialogVisible = true;
			app.selectedTask='';
			app.taskGroupDatas=[{id:0,name:'无所属分组'}]
			app.selectedTaskGroup=0
		}
		,queryTask:function(){
			$.ajax({
				url :staticURL+'/testcase/testTask/listAll?activeFlag=1',
				dataType : 'json',
				success : function(json) {
					app.taskDatas = json;
				}
			});
		}
		,joinTask:function(){
			if(app.selectedTask){
				var ids = new Array();
				for(var i=0;i<app.selectedCase.length;i++){
					ids.push(app.selectedCase[i].id)
				}
				$.ajax({
					url : staticURL+"/testcase/taskCase/addBatch?taskId="+app.selectedTask+"&caseIds="+ids+"&taskGroupId="+app.selectedTaskGroup,
					dataType : 'json',
					success : function(r) {
						if (r.success) {
							app.$message({showClose: true,
						          message: r.msg,
						          type: 'success',
						          position: 'top'});
							app.dialogConfig.taskDialogVisible = false;
						} else {
							app.$message({showClose: true,
						          message: r.msg,
						          type: 'error',
						          position: 'top'});
						}
					}
				});
			}
		}
	}
	,created: function () {
		this.init();
	}
	,computed:{
		libraryCode:function(){
			return this.queryCaseData.libraryCode;
		}
		,libraryNameShow:function(){
			return sy.subStringByteLength(this.libraryName,14);
		}
		,currentCaseId:function(){
			if(!app.currentCase || app.caseDatagrid.length==0){
				return null;
			}
			return app.currentCase.id;
		}
		,saveUrl:function(){
			if(app.operateFormName=='projectAddForm'){
				if(!app.isEdit){
					return staticURL+'/testcase/caseProject/add'
				}else{
					return staticURL+'/testcase/caseProject/edit'
				}
			}else if(app.operateFormName=='moduleAddForm'){
				if(!app.isEdit){
					return staticURL+'/testcase/caseModule/add'
				}else{
					return staticURL+'/testcase/caseModule/edit'
				}
			}else if(app.operateFormName=='caseAddForm'){
				if(!app.isEdit){
					return staticURL+'/testcase/caseLibrary/add'
				}else{
					return staticURL+'/testcase/caseLibrary/edit'
				}
			}
			return '';
		}
	}
});
});